*===============================================================================
*
*					WORKER BELIEFS ABOUT OUTSIDE OPTIONS
*		(c)	Simon Jaeger, Christopher Roth, Nina Roussille, Benjamin Schoefer
*							  2023 December 5
*						   	   SOEP-IAB Data 
*
*===============================================================================


********************************************************************************
*								Table I		 								   *
********************************************************************************

cap log close                     
log using ${log}/TableI.log, replace         

use "$data/unionsample_finaldata.dta", clear		

cap noisily keep if feff_2010_2017!=. & salary!=. 

* define bias measures 
cap drop bias_own_mover bias_mover_mover bias_own_median bias_own_rank bias_own_ml 
gen bias_own_mover = (ln_own_change-mean_delta_ln_wage_mover_2015) * 100
gen bias_own_ml = (ln_own_change-p_delta_ln_wage_mover) * 100
gen bias_mover_mover = (ln_mover_change-mean_delta_ln_wage_mover_2015) * 100
gen bias_own_median = d_guess_median_pct 
gen bias_own_rank = occupation_pctl_belief-occupation_pctl_true

su bias_*, d

replace ln_own_change = ln_own_change * 100
replace ln_mover_change = ln_mover_change * 100
replace salary_guess = salary_guess * 12 // annualise belief about median salary

* list of relevant variables for table 
local varlist age salary tenure female ft_employment pt_employment /// // Panel A: Demographics 
			  ln_own_change ln_mover_change occupation_pctl_belief salary_guess /// // Panel B: Beliefs
			  bias_own_mover bias_mover_mover bias_own_median bias_own_rank bias_own_ml // Panel C: Estimation Errors 

* create matrix / table  
matrix J=J(15,9,.)
local j = 0

foreach yvar of varlist `varlist' {
				
	* restrictions 
	local restriction "if `yvar'_w2!=."
		
	* generate var, winsorise
	cap drop `yvar'_w2
	winsor `yvar', gen(`yvar'_w2) p(0.02) 
	gen `yvar'_zro = `yvar'_w2==0 `restriction' 
	
	* first: print values in logfile					
	tabstat `yvar'_w2 `restriction', s(mean sd p10 p25 median p75 p90 n) format(%10.3f) 
	tabstat `yvar'_w2 `restriction' & ft_employment==1, s(mean sd p10 p25 median p75 p90 n) format(%10.3f) // print for summary table: only full time workers 	
	sum `yvar'_zro `restriction' 
	drop `yvar'_zro
	* second: store resolts for matrix 
	local j=`j'+1
	sum `yvar'_w2 `restriction', d 
	mat J[`j',1] = r(mean) // mean
	mat J[`j',2] = r(sd) // SD
	mat J[`j',3] = r(p10) // P10 
	mat J[`j',4] = r(p25) // P25
	mat J[`j',5] = r(p50) // Median 
	mat J[`j',6] = r(p75) // P75 
	mat J[`j',7] = r(p90) // P90 
	count `restriction' 
	local non_miss = r(N)
	mat J[`j',9] = `non_miss' // Non-Missing N 
	count `restriction' & `yvar'_w2==0 
	mat J[`j',8] = r(N)/`non_miss' // Share 0 			

} // varlist loop 

* create matrix / output table		
cap noisily{
	frmttable using "${log}/TableI.tex", tex fragment statmat(J) replace ///
	ctitle("" , "Mean" ,"SD", "P10", "P25", "Median","P75","P90","Share 0","$ N $") ///
	sdec(1,1,1,1,1,1,1,3,0\0,0,0,0,0,0,0,3,0\1,1,1,1,1,1,1,3,0\3,3,3,3,3,3,3,3,0\3,3,3,3,3,3,3,3,0\3,3,3,3,3,3,3,3,0\1,1,1,1,1,1,1,3,0\1,1,1,1,1,1,1,3,0\1,1,1,1,1,1,1,3,0\0,0,0,0,0,0,0,3,0\1,1,1,1,1,1,1,3,0\1,1,1,1,1,1,1,3,0\1,1,1,1,1,1,1,3,0\1,1,1,1,1,1,1,3,0\1,1,1,1,1,1,1,3,0) /// 
	rtitles("\textbf{Panel A: Demographics and Labor Market Characteristics} \\ & \\ Age in Years" ///
	\ "Wage (in EUR, per Year)" \ "Tenure in Years" \ "Share of Women" ///
	\ "Full-time Employed" \ "Part-time Employed" ///
	\ "\\ & \\ \textbf{Panel B: Beliefs} \\ & \\ Own Wage Change as \% of Wage" ///
	\ "Coworker Wage Change as \% of Wage" \ "Own Wage Rank in Same Occupation" ///
	\ "Median Wage in Occupation (in EUR, per Year)" /// 
	\ "\\ & \\ \textbf{Panel C: Estimation Errors} \\ & \\ Belief (Own Wage Change) Versus Coworker Wage Changes (\%)" ///
	\ "Belief (Coworker Wage Change) Versus Coworker Wage Changes (\%)" ///
	\ "Belief (Median Wage) Versus Actual Median Wage (\%)" /// 
	\ "Belief (Own Wage Rank in Occupation) Minus Actual Wage Rank (ppt/rank)" /// 
	\ "Belief (Own Wage Change) Versus ML Prediction (\%)" ) 
}


log close
clear